/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BuscarClienteForm.java
 *
 * Created on 09/04/2010, 10:52:21 AM
 */

package agencia.form;

import agencia.Agencia;
import agencia.Cliente;
import agencia.Juridica;
import agencia.Natural;
import agencia.controller.ClienteController;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author elvis
 */
public class BuscarClienteForm extends javax.swing.JInternalFrame {

    private ClienteController clienteController;
    private Agencia agencia;
    private ReservaTour reservaTour;

    public void setClienteController(ClienteController clienteController) {
        this.clienteController = clienteController;
    }
    
    public void setAgencia(Agencia agencia) {
        this.agencia = agencia;
    }

    /** Creates new form BuscarClienteForm */
    public BuscarClienteForm() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupo = new javax.swing.ButtonGroup();
        lblNombre = new javax.swing.JLabel();
        txtBnombre = new javax.swing.JTextField();
        rbNatural = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        lblcodigo = new javax.swing.JLabel();
        rbJuridica = new javax.swing.JRadioButton();
        txtNacionalidad = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtNacimiento = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtRuc = new javax.swing.JTextField();
        btnregistrar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblClientes = new javax.swing.JTable();

        setClosable(true);
        setTitle("Nuevo Cliente");

        lblNombre.setText("Nombre");

        grupo.add(rbNatural);
        rbNatural.setSelected(true);
        rbNatural.setText("Persona Natural");
        rbNatural.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbNaturalActionPerformed(evt);
            }
        });

        jLabel2.setText("Codigo");

        lblcodigo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        grupo.add(rbJuridica);
        rbJuridica.setText("Persona Juridica");
        rbJuridica.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbJuridicaActionPerformed(evt);
            }
        });

        jLabel3.setText("Nacionalidad");

        jLabel4.setText("F. de Nacimiento");

        jLabel5.setText("RUC");

        txtRuc.setEnabled(false);

        btnregistrar.setText("Registrar");
        btnregistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnregistrarActionPerformed(evt);
            }
        });

        jButton1.setText("Seleccionar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        tblClientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Codigo", "Nombre", "RUC", "Nacionalidad", "Nacimiento"
            }
        ));
        jScrollPane1.setViewportView(tblClientes);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(rbNatural)
                        .addGap(18, 18, 18)
                        .addComponent(rbJuridica))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNacimiento, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                            .addComponent(txtNacionalidad, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNombre)
                            .addComponent(jLabel5)
                            .addComponent(jLabel2))
                        .addGap(56, 56, 56)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(txtRuc, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                                        .addGap(18, 18, 18)
                                        .addComponent(btnregistrar, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(lblcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addComponent(txtBnombre, javax.swing.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE))))
                .addGap(10, 10, 10))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbJuridica)
                    .addComponent(rbNatural))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombre)
                    .addComponent(txtBnombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtRuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnregistrar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNacionalidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtNacimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rbJuridicaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbJuridicaActionPerformed
        // TODO add your handling code here:
        txtNacimiento.setEnabled(false);
        txtNacionalidad.setEnabled(false);

        txtRuc.setEnabled(true);
        lblNombre.setText("Razon Social");
    }//GEN-LAST:event_rbJuridicaActionPerformed

    private void rbNaturalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbNaturalActionPerformed
        // TODO add your handling code here:
         txtNacimiento.setEnabled(true);
        txtNacionalidad.setEnabled(true);

        txtRuc.setEnabled(false);
        lblNombre.setText("Nombre");
    }//GEN-LAST:event_rbNaturalActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (tblClientes.getSelectedRow()!=-1){
            int cod = (Integer)tblClientes.getValueAt( tblClientes.getSelectedRow(), 0);
            Cliente cl = clienteController.obtenerCliente(cod);
            reservaTour.getTxtCliente().setText(cl.getNombre());
            reservaTour.getVentas().setCliente(cl);
            this.dispose();
         }else{
            JOptionPane.showMessageDialog(rootPane, "Debe seleccionar un cliente");
         }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnregistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnregistrarActionPerformed
        // TODO add your handling code here:
        try {
            Cliente cliente = null;
            if(rbNatural.isSelected()){
                 cliente = new Natural();
                 cliente.setTipoCliente(Cliente.CLIENTE_TIPO_NATURAL);
                 ((Natural)cliente).setNacionalidad(txtNacionalidad.getText());
                 DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                 ((Natural)cliente).setFechaNacimiento(df.parse(txtNacimiento.getText()));
            }
            if(rbJuridica.isSelected()){
                cliente=new  Juridica();
                cliente.setTipoCliente(Cliente.CLIENTE_TIPO_JURIDICO);
                ((Juridica)cliente).setRuc(txtRuc.getText());
            }
            cliente.setNombre(txtBnombre.getText());
            cliente.setAgencia(agencia.getCodigo());
            
            clienteController.nuevoCliente(cliente);

            pintagrilla(clienteController.getClientes());
            txtBnombre.setText("");
            txtNacimiento.setText("");
            txtNacionalidad.setText("");
            txtRuc.setText("");
            lblcodigo.setText("");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }//GEN-LAST:event_btnregistrarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnregistrar;
    private javax.swing.ButtonGroup grupo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblcodigo;
    private javax.swing.JRadioButton rbJuridica;
    private javax.swing.JRadioButton rbNatural;
    private javax.swing.JTable tblClientes;
    private javax.swing.JTextField txtBnombre;
    private javax.swing.JTextField txtNacimiento;
    private javax.swing.JTextField txtNacionalidad;
    private javax.swing.JTextField txtRuc;
    // End of variables declaration//GEN-END:variables


    public void pintagrilla(List<Cliente> clientes) {
        try {
        DefaultTableModel tabla = (DefaultTableModel) tblClientes.getModel();
        tabla.setRowCount(0);
        //List<Cliente> examenes = agenciacontroller.getClientes();
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        for (Cliente cliente : clientes) {
            Object[] fila = new Object[5];
            fila[0] = cliente.getCodigo();
            fila[1] = cliente.getNombre();
            if ( cliente.getTipoCliente() == Cliente.CLIENTE_TIPO_NATURAL) {
                fila[2] = "";
                fila[3] = ((Natural)cliente).getNacionalidad();
                fila[4] = dateFormat.format( ((Natural)cliente).getFechaNacimiento() );
            }else{
                fila[2] = ((Juridica)cliente).getRuc();
                fila[3] = "";
                fila[4] = "";
            }
            tabla.addRow(fila);
        }

         } catch (Exception e) {
             e.printStackTrace();
        }

    }

    /**
     * @param reservaTour the reservaTour to set
     */
    public void setReservaTour(ReservaTour reservaTour) {
        this.reservaTour = reservaTour;
    }

    public void iniciaForm(){
        pintagrilla(clienteController.getClientes());
        
    }
       

}
